package com.tdk.bts.view.manager;

import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.tdk.bts.action.TheaterAction;
import com.tdk.bts.util.ImageUtil;
import com.tdk.bts.util.Request;

public class TheaterAddView extends JFrame{
	private JTextField nameField;
	private JTextField rowField;
	private JTextField colField;
	
	private TheaterAction action = new TheaterAction();

	public TheaterAddView(final TheaterManagerView view) {
		setTitle("添加放映厅");
		setSize(400, 406);
		setLocationRelativeTo(null);
		setLayout(null);
		
		Font textFont = new Font("宋体", Font.BOLD, 20);
		
		JLabel nameLabel = new JLabel("放映厅名称");
		nameLabel.setBounds(32, 64, 105, 24);
		nameLabel.setFont(textFont);
		add(nameLabel);
		
		nameField = new JTextField();
		nameField.setBounds(147, 65, 202, 21);
		nameField.setFont(textFont);
		add(nameField);
		nameField.setColumns(10);
		
		JLabel rowLabel = new JLabel("总排数");
		rowLabel.setBounds(32, 127, 89, 24);
		rowLabel.setFont(textFont);
		add(rowLabel);
		
		rowField = new JTextField();
		rowField.setBounds(147, 128, 202, 21);
		rowField.setFont(textFont);
		add(rowField);
		rowField.setColumns(10);
		
		JLabel colLabel = new JLabel("总列数");
		colLabel.setBounds(32, 194, 70, 24);
		colLabel.setFont(textFont);
		add(colLabel);
		
		colField = new JTextField();
		colField.setBounds(147, 195, 202, 21);
		colField.setFont(textFont);
		add(colField);
		colField.setColumns(10);
		
		JLabel submitBtn = new JLabel();
		submitBtn.setIcon(ImageUtil.getImage("addBtn.png"));
		submitBtn.setBounds(141, 275, 171, 60);
		add(submitBtn);
		
		submitBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				
				String name = nameField.getText();
				if(name == null || "".equals(name.trim()))
				{
					JOptionPane.showMessageDialog(null, "请输入放映室名", "错误", JOptionPane.ERROR_MESSAGE);
					return;
				}
				
				String rows = rowField.getText();
				if(rows == null || !rows.matches("\\d+"))
				{
					JOptionPane.showMessageDialog(null, "请输入放映室行数", "错误", JOptionPane.ERROR_MESSAGE);
					return;
				}
				
				String cols = colField.getText();
				if(cols == null || !cols.matches("\\d+"))
				{
					JOptionPane.showMessageDialog(null, "请输入放映室列数", "错误", JOptionPane.ERROR_MESSAGE);
					return;
				}
				
				Request request = new Request();
				request.setParameter("name", name);
				request.setParameter("rows", rows);
				request.setParameter("cols", cols);
				
				try {
					action.save(request);
					JOptionPane.showMessageDialog(null, "保存成功", "消息", JOptionPane.INFORMATION_MESSAGE);
					//直接用this获取的是内部类对象，类名.this获取的是外部类对象
					TheaterAddView.this.dispose();
					view.bindData(1);
				} catch (Exception e1) {
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, e1.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
				}
				
			}
		});
		setVisible(true);
	}
}
